<template>
  <div id="cloud-el-search">
    <el-input class="cloud-el-input" clearable v-model="param.oauthRefreshTokenId" placeholder="Please input oauthRefreshTokenId" />
    <el-input class="cloud-el-input" clearable v-model="param.tokenId" placeholder="Please input tokenId" />
    <el-input class="cloud-el-input" clearable v-model="param.username" placeholder="Please input username" />
    <el-input class="cloud-el-input" clearable v-model="param.clientId" placeholder="Please input clientId" />
    <el-input class="cloud-el-input" clearable v-model="param.remoteAddress" placeholder="Please input remoteAddress" />
    <el-input class="cloud-el-input" clearable v-model="param.scope" placeholder="Please input scope" />
    <el-input class="cloud-el-input" clearable v-model="param.redirectUri" placeholder="Please input redirectUri" />
    <el-input class="cloud-el-input" clearable v-model="param.refreshToken" placeholder="Please input refreshToken" />
    <el-input class="cloud-el-input" clearable v-model="param.sessionId" placeholder="Please input sessionId" />
    <el-input class="cloud-el-input" clearable v-model="param.state" placeholder="Please input state" />
    <el-button class="cloud-el-search" @click="cloudSearch">搜索</el-button>
    <el-button class="cloud-el-reset" @click="cloudClearable">重置</el-button>
    <el-button class="cloud-el-remove" @click="cloudRemove">删除</el-button>
  </div>
  <el-container>
    <el-table stripe :data="tableData" v-loading="loading" height="460" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="oauthRefreshTokenId" label="oauthRefreshTokenId" width="175"/>
      <el-table-column prop="tokenId" label="tokenId" width="300"/>
      <el-table-column prop="username" label="username" width="130"/>
      <el-table-column prop="clientId" label="clientId" width="160"/>
      <el-table-column prop="remoteAddress" label="remoteAddress" width="130"/>
      <el-table-column prop="createDate" label="createDate" width="160"/>
      <el-table-column prop="updateDate" label="updateDate" width="160"/>
      <el-table-column prop="deleted" label="deleted" width="100"/>
      <el-table-column prop="scope" label="scope" width="130"/>
      <el-table-column prop="redirectUri" label="redirectUri" width="260" :show-overflow-tooltip="true"/>
      <el-table-column prop="responseType" label="responseType" width="130"/>
      <el-table-column prop="tokenJson" label="tokenJson" width="160" :show-overflow-tooltip="true"/>
      <el-table-column prop="authenticationJson" label="authenticationJson" width="160" :show-overflow-tooltip="true"/>
      <el-table-column prop="authoritiesJson" label="authoritiesJson" width="160" :show-overflow-tooltip="true"/>
      <el-table-column prop="refreshToken" label="refreshToken" width="130" :show-overflow-tooltip="true"/>
      <el-table-column prop="expiration" label="expiration" width="160"/>
      <el-table-column prop="sessionId" label="sessionId" width="300"/>
      <el-table-column prop="state" label="state" width="300"/>
      <el-table-column fixed="right" label="Operations" width="100">
        <template #default="scope">
          <el-button type="text" size="small" v-if="scope.row.deleted" disabled>Delete</el-button>
          <el-button type="text" size="small" v-else @click="deleteRefreshTokenId(scope.row.oauthRefreshTokenId)">Delete</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-container>
  <el-container>
    <el-pagination layout="prev, pager, next, total" @current-change="currentChange" :total="param.total" />
  </el-container>
</template>

<script setup lang="ts">
import { page, removeById, removeByIds } from '../../api/audit/refresh-token'
import { ref, reactive } from 'vue'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'

// 缓存
const store = useStore()

// 表格数据
const tableData = ref([])

// 多选
const multipleSelection = ref<any[]>([])
// 多选主键
const oauthRefreshTokenIds = ref<number[]>([])

// 搜索参数
const param = reactive({
  current: 1,
  size: 10,
  total: 0,
  oauthRefreshTokenId: null,
  tokenId: null,
  username: null,
  clientId: null,
  remoteAddress: null,
  scope: null,
  redirectUri: null,
  refreshToken: null,
  sessionId: null,
  state: null
})

// 加载
const loading = ref(true)

// 搜索
const cloudSearch = () => {
  loading.value = true
  page(param).then(response => {
    console.log(response)
    if (response.code === store.state.settings.okCode) {
      const data = response.data
      tableData.value = data.records
      param.total = data.total
      loading.value = false
    } else {
      ElMessage.error(response.msg)
    }
  })
}

// 重置（清空搜索条件）
// Q：为何不使用 reset？
// A：因为使用 reset 后，页面不显示了，但是值还在，影响搜索
const cloudClearable = () => {
  param.oauthRefreshTokenId = null
  param.tokenId = null
  param.username = null
  param.clientId = null
  param.remoteAddress = null
  param.scope = null
  param.redirectUri = null
  param.refreshToken = null
  param.sessionId = null
  param.state = null
}

// 批量删除
const cloudRemove = () => {
  if (multipleSelection.value.length === 0) {
    ElMessage({
      message: '请先选择数据',
      // 显示时间，单位为毫秒。设为 0 则不会自动关闭，类型：number，默认值：3000
      duration: 1500,
      type: 'error'
    })
  } else {
    removeByIds(oauthRefreshTokenIds.value).then(response => {
      if (response.code === store.state.settings.okCode) {
        ElMessage({
          message: response.msg,
          // 显示时间，单位为毫秒。设为 0 则不会自动关闭，类型：number，默认值：3000
          duration: 1500,
          type: 'success',
          onClose: () => {
            cloudSearch()
          }
        })
      } else {
        ElMessage({
          message: response.msg,
          // 显示时间，单位为毫秒。设为 0 则不会自动关闭，类型：number，默认值：3000
          duration: 1500,
          type: 'error',
          onClose: () => {
            cloudSearch()
          }
        })
      }
    })
  }
}

// 初始搜索
cloudSearch()

// 改变时触发
const currentChange = (e: number) => {
  param.current = e
  cloudSearch()
}

// 删除刷新Token
const deleteRefreshTokenId = (e: number) => {
  removeById(e).then(response => {
    if (response.code === store.state.settings.okCode) {
      ElMessage({
        message: response.msg,
        // 显示时间，单位为毫秒。设为 0 则不会自动关闭，类型：number，默认值：3000
        duration: 1500,
        type: 'success',
        onClose: () => {
          cloudSearch()
        }
      })
    } else {
      ElMessage({
        message: response.msg,
        // 显示时间，单位为毫秒。设为 0 则不会自动关闭，类型：number，默认值：3000
        duration: 1500,
        type: 'error',
        onClose: () => {
          cloudSearch()
        }
      })
    }
  })
}

// 选择事件
const handleSelectionChange = (val: any[]) => {
  multipleSelection.value = val

  // 清空
  oauthRefreshTokenIds.value = []
  for (const i in val) {
    oauthRefreshTokenIds.value[i] = multipleSelection.value[i].oauthRefreshTokenId
  }
}

</script>

<style scoped>

.cloud-el-input {
  width: 300px;
}

.cloud-el-input,
.cloud-el-search,
.cloud-el-reset,
.cloud-el-remove {
  margin-left: 5px;
  margin-right: 5px;
}

</style>
